package xyz.xtt.exchange.anno;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AnnotationTypeFilter;

@Configuration
public class AnnotationScannerConfig {
	@Bean
	public Map<String, Class<?>> myCustomAnnotationMap() {
		Map<String, Class<?>> annotationMap = new HashMap<>();

		ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
		scanner.addIncludeFilter(new AnnotationTypeFilter(MyCustomAnnotation.class));
		Set<BeanDefinition> beans = scanner.findCandidateComponents("xyz.xtt.exchange"); // 替换为你的包路径
		for (BeanDefinition bean : beans) {
			try {
				Class<?> clazz = Class.forName(bean.getBeanClassName());
				MyCustomAnnotation annotation = clazz.getAnnotation(MyCustomAnnotation.class);
				if (annotation != null) {
					annotationMap.put(annotation.value(), clazz);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return annotationMap;
	}
}
